import { Service } from "../../services/service";
import { ComponentData } from "maishu-jueying";
import { PageDataRecord } from "entities";
import { ComponentInfo } from "components/common/component";
import { contextName } from "json!websiteConfig";


export class LocalService extends Service {
    private url(path: string): string {
        let contexts = requirejs.exec("contexts");
        let context: RequireContext = contexts[contextName];
        if (context != null && context.config != null && context.config.baseUrl != null) {
            return `${context.config.baseUrl}${path}`;
        }
        return `${path}`;
    }
    async clientFiles(): Promise<string[]> {
        let r = await this.get<string[]>(this.url(`./clientFiles`));
        return r;
    }
    async getPageDataByName(name: string): Promise<ComponentData> {
        debugger
        let record = await this.get<PageDataRecord>(this.url("./page-data/item"), { name });
        if (record == null)
            return null;

        return record.pageData;
    }
    async getPageDataById(id: string): Promise<ComponentData> {
        let record = await this.get<PageDataRecord>("./page-data/item", { id });
        if (record == null)
            return null;

        return record.pageData;
    }
    async getComponentInfos(): Promise<ComponentInfo[]> {
        let r = await this.get<ComponentInfo[]>(this.url("user/componentInfos"));
        return r;
    }
}